package stack_queue.leetcode;

import java.util.Stack;

/**
 * 双栈实现最小栈
 * @author yuisama
 * @date 2022/01/23 12:29
 **/
public class MinStack {
    // s1是实际存储元素的栈
    private Stack<Integer> s1 = new Stack<>();
    // s2是存储最小值的栈
    private Stack<Integer> s2 = new Stack<>();

    public MinStack() {

    }

    public void push(int val) {
        s1.push(val);
        if (s2.isEmpty()) {
            s2.push(val);
        }else {
            // 比较当前val和s2的栈顶元素，将最小值入栈
            int tmp = s2.peek();
            s2.push(Math.min(tmp,val));
        }
    }

    public void pop() {
        s1.pop();
        s2.pop();
    }

    public int top() {
        return s1.peek();
    }

    public int getMin() {
        return s2.peek();
    }
}